package com.ai.networking.IXCom;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class IXComServerTangent {

    public static List<TongXinThreadTangent> list = new ArrayList<>();

    public static int size = 0;

    public void start() {
        new Thread(() -> {
            try {
                //启动服务
                ServerSocket serverSocket = new ServerSocket(CommonIXCom.SYS_SERVER_PORT);
                log.info("================与L3对接的 Socket服务端已启动================");
                //循环等待客户端连接
                while (true) {
                    log.info("================等待 L3 连接服务器================");
                    Socket s = serverSocket.accept();
                    TongXinThreadTangent t = new TongXinThreadTangent(s);
                    t.start();
                    synchronized (list) {
                        list.add(t);
                        log.info("================ L3 服务端 第" + (++size) + "次被 L3 客户端连接================");
                    }
                }
            } catch (Exception e) {
                log.error("无法启动 L3对接的 服务端，或 L3对接的 服务端已经停止:" + e.getMessage());
            }
        }).start();
    }

    public static void main(String[] args) {
        IXComServerTangent server = new IXComServerTangent();
        server.start();
    }
}